<script setup>

import { userLoginService } from '@/api/user.js'
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'

const form = ref()

const formModel = ref({
  userName: '',
  password: ''
})

const rules = {
  userId: [
    //非空校验
    { required: true, message: '不输入用户名想干嘛', trigger: 'blur' },
    //长度校验
    {
      min: 5,
      max: 10,
      message: '名字怎么那么长,5-10位字符就好了',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '不输入密码想干嘛', trigger: 'blur' },
    //正则校验
    {
      //正则规则
      pattern: /^\S{6,15}$/,
      message: '密码设这么短想被盗是吧,给我写6-15位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    { required: true, message: '再输一遍防止你记错密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码设这么短想被盗是吧,给我写6-15位的非空字符',
      trigger: 'blur'
    },
    // 自定义校验(检验两次密码输入的是否一致)
    {
      validator: (rule, value, callback) => {
        // 判断 value 和 当前 form 中收集的 password 是否一致
        if (value !== formModel.value.password) {
          callback(new Error('龟儿两次密码输入的不一样,爬去重写'))
        } else {
          callback() // 就算校验成功，也需要callback
        }
      },
      trigger: 'blur'
    }
  ]
}

const userStore = useUserStore()
const router = useRouter()
const login = async () => {
  await form.value.validate()
  console.log('开始登录请求')
  const res = await userLoginService(formModel.value)
  userStore.setToken(res.data.data)
  alert('登录成功')
  router.push('/')
}
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg1"></el-col>
    <el-col :span="6" :offset="3" class="form">

      <!--登录相关表单-->
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        size="large"
        autocomplete="off"
      >
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="userName">
          <el-input
            v-model="formModel.userName"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox>记住我</el-checkbox>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            @click="login"
            class="button"
            type="primary"
            auto-insert-space
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg1 {
    background:
      url('@/assets/member.png') no-repeat 50% bottom / 500px auto,
      url('@/assets/login_bg.png') no-repeat center / cover;
    border-radius: 0 20px 20px 0;
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
